//
//  UIColor+Gradient.m
//  Gxj
//
//  Created by 刘伟 on 2017/10/16.
//  Copyright © 2017年 wkq. All rights reserved.
//

#import "UIColor+Gradient.h"
#import "UIColor+Hex.h"

@implementation UIColor (Gradient)

+ (CAGradientLayer *)setGradualChangingColor:(UIView *)view fromColor:(NSString *)fromHexColorStr toColor:(NSString *)toHexColorStr{
    
    CGFloat pathWidth = view.bounds.size.width;
    CGFloat halfWidth = pathWidth / 2;
    CGFloat layerWidth = pathWidth;
    
    //设置画笔路径
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(halfWidth, halfWidth)
                                                        radius:(pathWidth - 2.5)/2
                                                    startAngle:-M_PI/2*3
                                                      endAngle:M_PI/2
                                                     clockwise:YES];
    //[UIBezierPath bezierPathWithArcCenter:CGPointMake(view.frame.size.width/2.0, view.frame.size.height/2.0) radius:view.frame.size.width/2.0 - 10 startAngle:- M_PI_2 endAngle:-M_PI_2 + M_PI * 2 clockwise:YES];
    
    //创建背景圆环
    CAShapeLayer *trackLayer = [CAShapeLayer layer];
    trackLayer.frame = view.bounds;
    //清空填充色
    trackLayer.fillColor = [UIColor clearColor].CGColor;
    //设置画笔颜色 即圆环背景色
    trackLayer.strokeColor =  [UIColor colorWithRed:170/255.0 green:210/255.0 blue:254/255.0 alpha:1].CGColor;
    trackLayer.lineWidth = 1;
    
    CGFloat layerX = (view.bounds.size.width - layerWidth)/2;
    trackLayer.frame = CGRectMake(layerX, layerX, layerWidth, layerWidth);
    trackLayer.fillColor = [UIColor clearColor].CGColor;
//    trackLayer.strokeColor = color.CGColor;
    trackLayer.lineCap = kCALineCapButt;
    
    //path 决定layer将被渲染成何种形状
    trackLayer.path = path.CGPath;
    [view.layer addSublayer:trackLayer];
    
    //  CAGradientLayer类对其绘制渐变背景颜色、填充层的形状(包括圆角)
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = view.bounds;
    //  创建渐变色数组，需要转换为CGColor颜色
    gradientLayer.colors = @[(__bridge id)[UIColor colorWithRed:145.0/255.0 green:178.0/255.0 blue:250.0/255.0 alpha:1].CGColor,(__bridge id)[UIColor colorWithRed:212.0/255.0 green:142.0/255.0 blue:250.0/255.0 alpha:1].CGColor];
    //  设置渐变颜色方向，左上点为(0,0), 右下点为(1,1)
    [gradientLayer setStartPoint:CGPointMake(0.5, 1.0)];
    [gradientLayer setEndPoint:CGPointMake(0.5, 0.0)];
    //  设置颜色变化点，取值范围 0.0~1.0
    gradientLayer.locations = @[@0,@1];
    
    return gradientLayer;
}


@end
